package com.example.waterfall.adapter;

import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.example.waterfall.R;
import com.example.waterfall.model.BitmapInfo;

import com.example.waterfall.util.ImageFetcher;
import com.example.waterfall.view.ScaleImageView;
import com.example.waterfall.view.XListView;

public class WaterfallAdapter extends BaseAdapter  {
	private static final String TAG = "WaterfallAdapter";
	private LinkedList<BitmapInfo> mInfos;
	private ImageFetcher mImageFetcher;
	private ViewHolder holder;

	public WaterfallAdapter(Context context, XListView xListView,
			Handler handler,ImageFetcher imageFetcher) {
		mInfos = new LinkedList<BitmapInfo>();
		int columnCount = xListView.getColumnCount();// 获取列数
		mImageFetcher = imageFetcher;
		mImageFetcher.setLoadingImage(R.drawable.empty_photo);

	}

	/**
	 * 添加一个数据
	 * 
	 * @param datas
	 */
	public void addItem(BitmapInfo datas) {
		mInfos.add(datas);
		notifyDataSetChanged();
	}

	/**
	 * 加入数据
	 * 
	 * @param datas
	 */
	public void addItemLast(List<BitmapInfo> datas) {
		mInfos.addAll(datas);
	}

	/**
	 * 加在在队列前
	 * 
	 * @param datas
	 */
	public void addItemTop(List<BitmapInfo> datas) {
		for (BitmapInfo info : datas) {
			mInfos.addFirst(info);
		}
	}

	@Override
	public int getCount() {

		return mInfos.size();
	}

	@Override
	public Object getItem(int position) {

		return mInfos.get(position - 1);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final BitmapInfo info = mInfos.get(position);
		if (convertView == null) {
			LayoutInflater layoutInflator = LayoutInflater.from(parent
					.getContext());
			convertView = layoutInflator.inflate(R.layout.waterfall_pic_item,
					null);
			holder = new ViewHolder();
			holder.imageView = (ScaleImageView) convertView
					.findViewById(R.id.waterfall_pic_iv_image);
			holder.iamgeTitle = (TextView) convertView
					.findViewById(R.id.waterfall_pic_tv_tips);
			holder.good = (Button) convertView
					.findViewById(R.id.waterfall_pic_bt_good);
			convertView.setTag(holder);
		}
		holder = (ViewHolder) convertView.getTag();
		

		// 预设图片宽高
		holder.imageView.setImageWidth(info.imageWidth);
		holder.imageView.setImageHeight(info.imageHeight);
		// 设置将要缩放的宽高比
		// 计算

		mImageFetcher.setImageSize(info.imageWidth, info.imageHeight);
//		mImageFetcher.setImageSize(100);
		mImageFetcher.setFitScreen(10);
		mImageFetcher.loadImage(info.icon, holder.imageView);
		holder.iamgeTitle.setText(position+"标题"+info.imageWidth); 
		holder.good.setText(position+"赞"+info.good);
		
//		Log.d(TAG, position+":"+info.good+":"+holder.good.toString());
		
//		setOnListener(holder.good);
		holder.good.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO bug 这个被复用了，该如何让他依次指定
				//lsitview局限性。
//				holder.good.setText("赞了");
				info.good = info.good+1;
				notifyDataSetChanged();
				
				Log.d(TAG, position+":"+info.good+":"+holder.good.toString());
			}
		});
		// 返回一个图片

		return convertView;
	}

	class ViewHolder {
		public ScaleImageView imageView;
		public TextView iamgeTitle;
		public Button good;
	}
	
	
//	/**
//	 * 设置点击监听，
//	 * @param convertView 
//	 */
//	private void setOnListener(View view) {
//		
//		view.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				switch (v.getId()) {
//				case R.id.waterfall_pic_bt_good:
//					Log.i(TAG, "点");
//					holder.good.setText("赞了");
//					notifyDataSetChanged();
//					break;
//
//				default:
//					break;
//				}
//				
//				
//			}
//		});
//		
//	}
}
